package com.xinghanlaser.applet.controller;

import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.xinghanlaser.applet.base.auth.vo.UserDataVO;
import com.xinghanlaser.applet.base.common.entity.ResponseEntity;
import com.xinghanlaser.applet.base.oss.model.UploadObjectResult;
import com.xinghanlaser.applet.entity.PointState;
import com.xinghanlaser.applet.query.TopicQuery;
import com.xinghanlaser.applet.service.ITopicService;
import com.xinghanlaser.applet.vo.applet.ProductDetailVO;
import com.xinghanlaser.applet.vo.applet.TopicCountVO;
import com.xinghanlaser.applet.vo.applet.TopicVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * <p>
 *
 * <p>
 *
 * @author: mftuo 陀铭峰
 * @since: 2024/5/8 9:09
 */
@RestController
@RequestMapping("/topic")
@Api(value = "申请单管理", tags = "申请单管理")
public class TopicController {
    @Resource
    private ITopicService service;

    @GetMapping("/product-detail/{productSn}")
    @ApiOperation("根据产品序列号获取产品详情")
    public ResponseEntity<ProductDetailVO> getProductDetail(@PathVariable("productSn")String productSn){
        ProductDetailVO vo = service.getProductDetail(productSn);
        return ResponseEntity.ok(vo);
    }

    @GetMapping("/{topicId}/{roleId}")
    @ApiOperation("根据ID查询工单")
    public ResponseEntity<TopicVO> getTopicById(@PathVariable("topicId")Long id, @PathVariable("roleId")Long roleId){
        return service.getTopicById(id, roleId);
    }

    @GetMapping("/page")
    @ApiOperation("根据条件分页查询申请单")
    public ResponseEntity<ResponseEntity.PageResult<TopicVO>> pageByCondition(@Valid TopicQuery query){
        return service.pageByCondition(query);
    }

    @PostMapping("/pictures")
    @ApiOperation("上传图片")
    public ResponseEntity<List<UploadObjectResult>> batchUploadPicture(@RequestParam("pictures") MultipartFile pictures){
        List<MultipartFile> collect = Stream.of(pictures).collect(Collectors.toList());

        return service.batchUploadPicture(collect);
    }

    @GetMapping("/getUserListByRoleId/{roleId}")
    @ApiOperation("根据角色ID获取该角色用户列表")
    public ResponseEntity<List<UserDataVO>> getUserListByRoleId(@PathVariable("roleId") Long roleId){
        return service.getUserListByRoleId(roleId);
    }

    @GetMapping("/getStateList")
    @ApiOperation("获取申请单状态列表")
    public ResponseEntity<List<PointState>> getStateList(@RequestParam(required = false) Long roleId){
        return service.getStateList(roleId);
    }

    @GetMapping("/all/{userId}/{roleId}")
    @ApiOperation("查询所有相关申请单主要获得数量")
    public ResponseEntity<List<TopicCountVO>> getTopicCountList(@PathVariable("userId")Long userId, @PathVariable("roleId")Long roleId){
        return service.getTopicCountList(userId, roleId);
    }
}
